<template>
  <div class="container">
    <!-- 用户个人资料 -->
    <div class="user-profile">
      <div class="info">
        <van-image round :src="user.photo" />
        <h3 class="name">
          {{ user.name }}
          <br />
          <van-tag v-if="!user.certi" size="mini">申请认证</van-tag>
        </h3>
      </div>
      <van-row>
        <van-col span="8">
          <p>{{ user.art_count }}</p>
          <p>动态</p>
        </van-col>
        <van-col span="8">
          <p>{{ user.follow_count }}</p>
          <p>关注</p>
        </van-col>
        <van-col span="8">
          <p>{{ user.fans_count }}</p>
          <p>粉丝</p>
        </van-col>
      </van-row>
    </div>

    <!-- 操作链接 -->
    <van-row class="user-links">
      <van-col span="8">
        <van-icon name="newspaper-o" color="#7af" />我的作品
      </van-col>
      <van-col span="8">
        <van-icon name="star-o" color="#f00" />我的收藏
      </van-col>
      <van-col span="8">
        <van-icon name="tosend" color="#fa0" />阅读历史
      </van-col>
    </van-row>

    <!-- 编辑入口 -->
    <van-cell-group class="user-group">
      <van-cell icon="edit" title="编辑资料" to="/user/profile" is-link />
      <van-cell icon="chat-o" title="小智同学" to="/user/chat" is-link />
      <van-cell icon="setting-o" title="系统设置" is-link />
      <van-cell icon="warning-o" title="退出登录" @click="logOut" is-link />
    </van-cell-group>
  </div>
</template>

<script>
// art_count: 0
// certi: ""
// fans_count: 0
// follow_count: 2
// id: 1285811921370480600
// intro: ""
// is_media: false
// like_count: 0
// name: "19801279521"
// photo: "http://toutiao.meiduo.site/Fkj6tQi3xJwVXi1u2swCElotfdCi"
// message: "OK"
import { user } from "@/api/user.js";
import { mapMutations } from "vuex";
export default {
  name: "UserIndex",
  data() {
    return {
      user: {}
    };
  },
  created() {
    this.getUser();
  },
  methods: {
    ...mapMutations(["savePhoto"]),
    async getUser() {
      const res = await user();
      const resData = res.data.data;
      this.user = resData;
      if (resData) {
        this.savePhoto(resData.photo);
      }
    },
    logOut() {
      // 1. 提示
      this.$dialog
        .confirm({
          title: "确认离开",
          message: "你要注销码？"
        })
        .then(() => {
          // on confirm
          console.log("confirm");

          // 2. clear token vuex localStorage
          this.$store.commit("clearToken");
          // 3. 路由的跳转
          this.$router.push("/login");
        })
        .catch(() => {
          // on cancel
          console.log("cancel");
        });
    }
  }
};
</script>

<style lang="less" scoped>
.user {
  &-profile {
    width: 100%;
    height: 200px;
    display: block;
    background: #3296fa;
    color: #fff;
    .info {
      display: flex;
      padding: 20px;
      align-items: center;
      .van-image {
        width: 64px;
        height: 64px;
      }
      .name {
        font-size: 16px;
        font-weight: normal;
        margin-left: 10px;
      }
      .van-tag {
        background: #fff;
        color: #3296fa;
      }
    }
    p {
      margin: 0;
      text-align: center;
    }
  }
  &-group {
    margin-bottom: 15px;
  }
  &-links {
    padding: 15px 0;
    font-size: 12px;
    text-align: center;
    background-color: #fff;
    .van-icon {
      display: block;
      font-size: 24px;
      padding-bottom: 5px;
    }
  }
}
</style>
